package com.zmc.order.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.order.infrastructure.dao.OrderItemDao;
import com.zmc.order.infrastructure.entity.OrderItemEntity;
import com.zmc.order.infrastructure.service.OrderItemService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {


    @Override
    public List<OrderItemEntity> listByOrderSns(String memberId, List<String> orderSns) {
        LambdaQueryWrapper<OrderItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderItemEntity::getMemberId, memberId)
                .in(OrderItemEntity::getOrderSn, orderSns);
        return list(wrapper);
    }

    @Override
    public List<OrderItemEntity> listByOrderSn(String orderSn) {
        LambdaQueryWrapper<OrderItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderItemEntity::getOrderSn, orderSn);
        return list(wrapper);
    }

    @Override
    @Transactional
    public void saveBatchItem(List<OrderItemEntity> itemList) {
        baseMapper.batchSaveOrderItems(itemList);
    }

    @Override
    public void batchUpdateOrderItems(List<OrderItemEntity> itemList) {
        baseMapper.batchUpdateOrderItems(itemList);
    }

    @Override
    @Transactional
    public void removeItemsByOrderSn(String memberId, String orderSn) {
        LambdaQueryWrapper<OrderItemEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderItemEntity::getMemberId, memberId)
                .eq(OrderItemEntity::getOrderSn, orderSn);
        remove(wrapper);
    }

    @Override
    @Transactional
    public void updateBatchByOrderAndSpu(String orderSn, List<OrderItemEntity> items) {
        baseMapper.updateBatchByOrderAndSpu(orderSn, items);
    }
}